package Q3;

import java.util.*;

class Process {
    String name;     // 进程名
    int arrivalTime; // 到达时间
    int burstTime;   // 所需运行时间
    int priority;    // 优先数
    int completionTime; // 完成时间
    int remainingTime; // 剩余运行时间

    public Process() {
    }

    Process(String name, int arrivalTime, int burstTime, int priority) {
        this.name = name;
        this.arrivalTime = arrivalTime;
        this.burstTime = burstTime;
        this.priority = priority;
        this.remainingTime = burstTime; // 初始化剩余时间
    }

    // 按优先级比较
    public int compareTo(Process other) {
        if (this.priority != other.priority) {
            return Integer.compare(other.priority, this.priority); // 优先数越大，优先级越高
        }
        return Integer.compare(this.arrivalTime, other.arrivalTime); // 优先数相同，按照到达时间
    }
}

public class Question3 {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        List<Process> processes = new ArrayList<>();

        // 输入四个进程的信息
        for (int i = 0; i < 4; i++) {
            String[] input = scanner.nextLine().split(" ");
            String name = input[0];
            int arrivalTime = Integer.parseInt(input[1]);
            int burstTime = Integer.parseInt(input[2]);
            int priority = Integer.parseInt(input[3]);
            processes.add(new Process(name, arrivalTime, burstTime, priority));
        }

        // 按到达时间排序
        processes.sort(Comparator.comparingInt(p -> p.arrivalTime));

        int currentTime = 0; // 当前时间
        Process process = new Process();
        Queue<Process> readyQueue = new PriorityQueue<>(Comparator.comparingInt(process::compareTo));
        int completedCount = 0; // 记录完成的进程数

        while (completedCount < processes.size()) {
            // 将已到达的进程加入就绪队列
            for (Process p : processes) {
                if (p.arrivalTime <= currentTime && p.remainingTime > 0) {
                    readyQueue.add(p);
                }
            }

            if (!readyQueue.isEmpty()) {
                // 从就绪队列获取优先级最高的进程
                Process currentProcess = readyQueue.poll();
                // 处理当前进程
                currentProcess.remainingTime--;
                currentTime++;

                // 检查当前进程是否完成
                if (currentProcess.remainingTime == 0) {
                    currentProcess.completionTime = currentTime;
                    completedCount++;
                }

                // 所有进程的优先数更新（运行态-1,就绪态+1）
                for (Process p : readyQueue) {
                    p.priority++;
                }
                currentProcess.priority--; // 当前进程的优先数减1
            } else {
                // 如果没有就绪进程，当前时间推进
                currentTime++;
            }
        }

        // 输出每个进程的完成时间
        for (Process p : processes) {
            System.out.print(p.completionTime + " ");
        }
    }
}